import VersionManager from "../../engine/VersionManager";
import ViewBase from "../../engine/ViewBase";
import GameController from "../../GameController";
import { NativeSdkHelper } from "../../sdk/NativeSdkHelper";
import { AudioHelper } from "../audio/AudioHelper";
import SettingModel from "../model/SettingModel";
import { UIHelper } from "../util/UIHelper";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewSetting extends ViewBase {

    private txtVersion: cc.Label
    private btnSound: cc.Node;
    private btnMusic: cc.Node;

    onEnable(): void {
        super.onEnable()
        let btnswtich: cc.Node = this.findChild(this.node, "btnswtich")
        UIHelper.onAddEvent(btnswtich, () => {
            this.closeView()
            GameController.instance.switchAccount()
        })
        this.txtVersion = this.getChild(this.node, "txtVersion", cc.Label)
        this.txtVersion.string = "V" + NativeSdkHelper.getAppVersion() + "~" + VersionManager.instance.nowVersion

        this.btnSound = this.findChild(this.node, "btnSound")
        this.btnMusic = this.findChild(this.node, "btnMusic")
        UIHelper.onAddEvent(this.btnMusic, () => {
            if (SettingModel.instance.musicVolumn == 1) {
                SettingModel.instance.musicVolumn = 0
                AudioHelper.stopMusic()
            } else {
                SettingModel.instance.musicVolumn = 1;
                AudioHelper.playMusic()
            }
            SettingModel.instance.saveToLocal()
            this.refresh()
        })
        UIHelper.onAddEvent(this.btnSound, () => {
            if (SettingModel.instance.soundVolum == 1) {
                SettingModel.instance.soundVolum = 0
            } else {
                SettingModel.instance.soundVolum = 1;
            }
            SettingModel.instance.saveToLocal()
            this.refresh()
        })
        this.refresh()
    }

    private refresh() {
        if (SettingModel.instance.musicVolumn == 1) {
            this.btnMusic.getChildByName("open").active = true
            this.btnMusic.getChildByName("close").active = false
        } else {
            this.btnMusic.getChildByName("open").active = false
            this.btnMusic.getChildByName("close").active = true
        }
        if (SettingModel.instance.soundVolum == 1) {
            this.btnSound.getChildByName("open").active = true
            this.btnSound.getChildByName("close").active = false
        } else {
            this.btnSound.getChildByName("open").active = false
            this.btnSound.getChildByName("close").active = true
        }

      
    }
}
